package com.sec.android.app.myfiles.module.search;

import android.content.res.Configuration;
import android.database.Cursor;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Checkable;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ListView;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.feature.CloudMgr;
import com.sec.android.app.myfiles.feature.MouseKeyboardMgr;
import com.sec.android.app.myfiles.feature.ViEffectMgr;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.fragment.filelist.FileListActionMenu;
import com.sec.android.app.myfiles.info.AppFeatures;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.log.SamsungAnalyticsLog;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.abstraction.FileRecord.CloudType;
import com.sec.android.app.myfiles.module.abstraction.FileRecord.StorageType;
import com.sec.android.app.myfiles.module.cloud.CloudFileRecord;
import com.sec.android.app.myfiles.navigation.NavigationManager;
import com.sec.android.app.myfiles.util.UiUtils;
import com.sec.android.app.myfiles.widget.listview.SelectionManager;

import java.util.ArrayList;
import java.util.List;

public class SearchFragment extends AbsMyFilesFragment implements OnItemLongClickListener, OnChildClickListener,
        OnGroupClickListener, CloudMgr.CloudStateListener {

    private ExpandableListView mListView;
    private SearchAdapter mAdapter;
    private FileRecord mPreRecord;
    private boolean mSelectAll = false;
    private boolean mIsFastScrolling = false;
    private long mPackedPosition;
    private View mEmptyView;
    private CloudMgr mCloudMgr;
    private int mExpandingGroupPosition;
    private int mSelectedGroupPosition;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.search_list_fragment, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mListView = (ExpandableListView) view.findViewById(R.id.search_list);
        SearchFileRecord curRecord = (SearchFileRecord) mNavigationInfo.getCurRecord();
        mPreRecord = curRecord.getPreRecord();
        mAdapter = SearchAdapterBuilder.createAdapter(mPreRecord, mContext, mNavigationInfo, this.getLoaderManager());
        mListView.setAdapter(mAdapter);
        mListView.setOnItemLongClickListener(this);
        mListView.setOnChildClickListener(this);
        mListView.setOnGroupClickListener(this);
        mListView.setGroupIndicator(null);
        mListView.setDivider(null);
        expandAllGroup();
        setFastScrollEnabled(true);
        setEmptyView();
        setActionMode(SelectionManager.getInstance(mProcessId).getActionModeType());

        MouseKeyboardMgr.setOnKeyListener(mListView, this, StorageType.Search);
        mCloudMgr = CloudMgr.getInstance(mContext);
        mCloudMgr.addListener(this);
        UiUtils.setToggleMenuItem(mContext, mMenu, mNavigationInfo, false);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isSelectActionMode()) {
            int savedGroupPosition = SelectionManager.getInstance(mProcessId).getSelectedGroupPosition();
            mPackedPosition = (savedGroupPosition == -1) ? -1 : ExpandableListView.getPackedPositionForGroup(savedGroupPosition);
            setItemsChecked(SelectionManager.getInstance(mProcessId).getSelectedPositions(), true);
            updateActionBarMenu();
            SelectionManager.getInstance(mProcessId).clear();
        }
        expandGroup(SelectionManager.getInstance(mProcessId).getExpandedGroupPositions());
    }

    @Override
    public void onPause() {
        super.onPause();
        if (isSelectActionMode()) {
            int savedGroupPosition = (mPackedPosition == -1) ? -1 : ExpandableListView.getPackedPositionGroup(mPackedPosition);
            SelectionManager.getInstance(mProcessId).saveGroupPosition(savedGroupPosition);
        }
    }

    @Override
    public void setActionMode(ActionModeType actionMode) {
        super.setActionMode(actionMode);
        if (mAdapter != null) {
            boolean showCheckbox = false;

            switch (actionMode) {
                case SELECT_FILE_FOLDER_OPERATION:
                    showCheckbox = true;
                    setChoiceMode(ExpandableListView.CHOICE_MODE_MULTIPLE);
                    break;
                default:
                    setChoiceMode(ExpandableListView.CHOICE_MODE_NONE);
                    selectAll(false);
                    SelectionManager.getInstance(mProcessId).clearSelectedRecords();
                    break;
            }

            if (isSelectActionMode() && showCheckbox) {
                View actionbar = mActivity.findViewById(R.id.action_select);
                ViEffectMgr.getInstance().showCheckBox(mContext, mListView, actionbar, true);
            }
        }
    }

    private void setFastScrollEnabled(boolean enabled) {
    }

    @Override
    public void setEmptyView() {
        ViewGroup root = (ViewGroup) getView();
        if (root == null) {
            Log.e(this, "root view is null");
            return;
        }
        ViewStub emptyViewStub = (ViewStub) root.findViewById(R.id.search_empty_view_stub);

        if (emptyViewStub != null) {
            mEmptyView = emptyViewStub.inflate();
        } else {
            mEmptyView = root.findViewById(R.id.search_empty_view);
        }
        mListView.setEmptyView(mEmptyView);
    }

    private void expandAllGroup() {
        for (int groupPosition = 0; groupPosition < mAdapter.getGroupCount(); groupPosition++) {
            mListView.expandGroup(groupPosition);
        }
    }

    private void expandGroup(List<Integer> expandedGroupPosition) {
        if (expandedGroupPosition != null && !expandedGroupPosition.isEmpty()) {
            for (Integer position : expandedGroupPosition) {
                mListView.expandGroup(position);
            }
        }
    }

    @Override
    public void reload() {
        if (mAdapter != null) {
            mAdapter.reload();
        }
    }

    @Override
    public ArrayList<FileRecord> getSelectedFile() {
        ArrayList<FileRecord> ret = new ArrayList<>();
        if (isNothingSelected()) {
            return ret;
        }

        SparseBooleanArray selectedChildArray = null;
        ArrayList<FileRecord> selectedRecord = SelectionManager.getInstance(mProcessId).getSelectedChildRecords();
        int selectedGroupPosition = SelectionManager.getInstance(mProcessId).getSelectedGroupPosition();
        if (!selectedRecord.isEmpty() && mListView != null && !mListView.isGroupExpanded(selectedGroupPosition)) {
            return new ArrayList<>(selectedRecord);
        }

        if (mListView != null) {
            if (mSelectAll) {
                selectedChildArray = getReverseSelectedChildArray(mListView.getCheckedItemPositions());
            } else {
                selectedChildArray = mListView.getCheckedItemPositions();
            }
        }

        if (selectedChildArray != null) {
            if (mSelectAll) {
                int groupPosition = ExpandableListView.getPackedPositionGroup(mPackedPosition);
                Cursor groupCursor = mAdapter.getGroup(groupPosition);
                Cursor childCursor;
                for (int flatPosition = 0; flatPosition < selectedChildArray.size(); flatPosition++) {
                    childCursor = mAdapter.getChild(groupPosition, flatPosition);
                    ret.add(mAdapter.getFileRecord(groupCursor, childCursor));
                }
            } else {
                for (int flatPosition = 0; flatPosition < selectedChildArray.size(); flatPosition++) {
                    if (selectedChildArray.valueAt(flatPosition)) {
                        ret.add(getFileRecord(selectedChildArray.keyAt(flatPosition)));
                    }
                }
            }
        }

        return ret;
    }

    private SparseBooleanArray getReverseSelectedChildArray(SparseBooleanArray selectedChildArray) {
        if (selectedChildArray == null || isNothingSelected()) {
            return new SparseBooleanArray();
        }

        int groupPosition = ExpandableListView.getPackedPositionGroup(mPackedPosition);
        int childCount = mAdapter.getChildrenCount(groupPosition);
        SparseBooleanArray reversedSelectedChildArray = null;
        if (childCount > selectedChildArray.size()) {
            reversedSelectedChildArray = new SparseBooleanArray(childCount - selectedChildArray.size());
            for (int i = 1; i < childCount + 1; i++) {
                if (!selectedChildArray.get(i)) {
                    reversedSelectedChildArray.put(i, true);
                }
            }
        }
        return reversedSelectedChildArray;
    }

    @Override
    public int getSelectedFileCount() {
        int ret = (mListView != null) ? mListView.getCheckedItemCount() : 0;

        if (mSelectAll && !isNothingSelected()) {
            ret = getTotalCheckableFileCount() - ret;
        }

        return ret;
    }

    @Override
    public int getTotalFileCount() {
        int ret = 0;

        if (mAdapter != null) {
            for (int groupPosition = 0; groupPosition < mAdapter.getGroupCount(); groupPosition++) {
                ret = ret + mAdapter.getChildrenCount(groupPosition);
            }
        }

        return ret;
    }

    @Override
    public int getTotalCheckableFileCount() {
        int ret = 0;
        if (isNothingSelected()) {
            return ret;
        }
        int groupPosition = ExpandableListView.getPackedPositionGroup(mPackedPosition);
        if (mAdapter != null && mListView != null) {
            if (mListView.isGroupExpanded(groupPosition)) {
                ret = mAdapter.getChildrenCount(groupPosition);
            } else {
                ret = mAdapter.getCollapsedChildrenCount(groupPosition);
            }
        }

        return ret;
    }

    @Override
    public FileRecord getFileRecord(int position) {
        FileRecord record = null;
        long packedPosition = mListView.getExpandableListPosition(position);
        int itemType = ExpandableListView.getPackedPositionType(packedPosition);
        if (itemType == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
            int groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition);
            int childPosition = ExpandableListView.getPackedPositionChild(packedPosition);
            if (groupPosition > -1 && childPosition > -1) {
                Cursor groupCursor = mAdapter.getGroup(groupPosition);
                Cursor childCursor = mAdapter.getChild(groupPosition, childPosition);
                record = mAdapter.getFileRecord(groupCursor, childCursor);
            }
        }

        if (record == null && position == 1) {
            record = getFirstVisibleChild();
        }

        return record;
    }

    private FileRecord getFirstVisibleChild() {
        FileRecord ret = null;
        int groupCount = mAdapter.getGroupCount();
        for (int i = 0; i < groupCount + 1; i++) {
            long packedPosition = mListView.getExpandableListPosition(i);
            int itemType = ExpandableListView.getPackedPositionType(packedPosition);
            if (itemType == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
                int groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition);
                int childPosition = ExpandableListView.getPackedPositionChild(packedPosition);
                if (groupPosition > -1 && childPosition > -1) {
                    Cursor groupCursor = mAdapter.getGroup(groupPosition);
                    Cursor childCursor = mAdapter.getChild(groupPosition, childPosition);
                    ret = mAdapter.getFileRecord(groupCursor, childCursor);
                    if (ret != null) {
                        break;
                    }
                }
            }
        }

        return ret;
    }

    @Override
    public void selectAll(boolean select) {
        clearSelectedFile();
        mSelectAll = select;
        mAdapter.selectAll(mSelectAll);
        mListView.invalidateViews();
        if (select) {
            SamsungAnalyticsLog.sendLog(mProcessId, SamsungAnalyticsLog.Event.SELECT_ALL, SamsungAnalyticsLog.SelectMode.SELECTION_MODE);
        } else {
            if (!mAdapter.isOnlyGroupShown()) {
                mPackedPosition = -1;
            }
            SelectionManager.getInstance(mProcessId).clearSelectedRecords();
        }
    }

    private void clearSelectedFile() {
        int checkedItemCount = mListView.getCheckedItemCount();
        if (checkedItemCount > 0) {
            setItemsChecked(mListView.getCheckedItemPositions(), false);
        }
    }

    @Override
    public void clearEmptyView() {
        if (mListView != null) {
            mListView.setEmptyView(null);
        }
    }

    @Override
    public void onLoadFinished() {
        if (mListView != null && mEmptyView != null) {
            int childCount = 0;
            for (int groupPosition = 0; groupPosition < mAdapter.getGroupCount(); groupPosition++) {
                childCount = childCount + mAdapter.getChildrenCount(groupPosition);
            }

            if (childCount == 0) {
                mListView.setVisibility(View.GONE);
                mEmptyView.setVisibility(View.VISIBLE);
            } else {
                mEmptyView.setVisibility(View.GONE);
                mListView.setVisibility(View.VISIBLE);
            }
        }

        if (isSelectActionMode() && mMenu != null) {
            updateActionBarMenu();
        }
    }

    @Override
    public void setLoadingView() {
        //Do nothing
    }

    @Override
    public int getListItemCount() {
        return getTotalFileCount();
    }

    @Override
    public AbsListView getListView() {
        return mListView;
    }

    @Override
    protected void _onContentChanged(int event) {
        if (isSelectActionMode()) {
            finishActionMode();
            closePopupDialog();
        }

        if (mAdapter != null) {
            mAdapter.reload();
        }
    }

    @Override
    public boolean onGroupClick(ExpandableListView ExpandableListView, View view, int groupPosition, long id) {
        boolean isSingle = mAdapter == null || mAdapter.getGroupCount() == 1;
        SamsungAnalyticsLog.sendLog(getProcessId(), SamsungAnalyticsLog.Event.RESULT_EXPAND, isSelectActionMode() ? SamsungAnalyticsLog.SelectMode.SELECTION_MODE : null);
        if (!isSingle) {
            setExpandingGroupPosition(groupPosition);
            if (groupPosition == ExpandableListView.getPackedPositionGroup(mPackedPosition) && mListView.isGroupExpanded(groupPosition)) {
                SelectionManager.getInstance(mProcessId).saveSelectedRecords(getSelectedFile());
                SelectionManager.getInstance(mProcessId).saveGroupPosition(groupPosition);
            }

            if (isSelectActionMode() && groupPosition < mSelectedGroupPosition) {
                if (mListView.isGroupExpanded(groupPosition)) {
                    updateSelectedChildPosition((-1) * mAdapter.getChildrenCount(groupPosition));
                } else {
                    updateSelectedChildPosition(mAdapter.getCollapsedChildrenCount(groupPosition));
                }
            }

            if (isNothingSelected() && isSelectActionMode()) {
                SelectionManager.getInstance(mProcessId).clearSelectedRecords();
            }
        }

        return isSingle;
    }

    private void updateSelectedChildPosition(int modifiedChildCount) {
        ArrayList<Integer> selectedPositions = new ArrayList<>();
        SparseBooleanArray selectedChild = mListView.getCheckedItemPositions();
        if (selectedChild != null && selectedChild.size() > 0) {
            for (int flatPosition = 0; flatPosition < selectedChild.size(); flatPosition++) {
                if (selectedChild.valueAt(flatPosition)) {
                    selectedPositions.add(selectedChild.keyAt(flatPosition) + modifiedChildCount);
                }
            }
        }
        mListView.clearChoices();
        for (int i = 0; i < selectedPositions.size(); i++) {
            mListView.setItemChecked(selectedPositions.get(i), true);
        }
    }

    @Override
    public boolean onChildClick(ExpandableListView parent, View view, int groupPosition, int childPosition, long id) {
        boolean handled = false;

        if (MouseKeyboardMgr.isShiftPressed()) {
            MouseKeyboardMgr.onShiftMouseClick(SearchFragment.this, mListView, childPosition + 1);
        } else if (MouseKeyboardMgr.isCtrlPressed()) {
            MouseKeyboardMgr.onCtrlMouseClick(SearchFragment.this, mListView, childPosition + 1);
        } else {
            if (isSelectActionMode()) { // Select mode : Select file and updateActionMenu
                int selectedChildPosition = parent.getFlatListPosition(
                        ExpandableListView.getPackedPositionForChild(groupPosition, childPosition));
                boolean check = !mListView.isItemChecked(selectedChildPosition);
                setItemChecked(selectedChildPosition, check);
                updateActionBarMenu();
                handled = true;
                mSelectedGroupPosition = ExpandableListView.getPackedPositionGroup(mPackedPosition);
            } else { // Normal mode : Execute file
                if (UiUtils.isValidClick(view.getId())) {
                    FileRecord record;
                    Cursor groupCursor = mAdapter.getGroup(groupPosition);
                    Cursor childCursor = mAdapter.getChild(groupPosition, childPosition);
                    record = mAdapter.getFileRecord(groupCursor, childCursor);

                    if (record != null) {
                        SamsungAnalyticsLog.sendLog(getProcessId(), record.isDirectory() ? SamsungAnalyticsLog.Event.SEARCH_FOLDER : SamsungAnalyticsLog.Event.SEARCH_FILE, null);

                        MyFilesFacade.executeRecord(mProcessId, mActivity, record);
                        handled = true;
                    }
                }
            }
        }

        return handled;
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        long packedPosition = mListView.getExpandableListPosition(position);
        int itemType = ExpandableListView.getPackedPositionType(packedPosition);

        if (!isSelectActionMode()
                && NavigationManager.isPossibleToActionMode()
                && itemType == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
            sendLogSA(position);
            mPackedPosition = packedPosition;
            setActionMode(ActionModeType.SELECT_FILE_FOLDER_OPERATION);
            mListView.clearChoices();
            setItemChecked(position, true);
            mSelectedGroupPosition = ExpandableListView.getPackedPositionGroup(packedPosition);
            updateActionBarMenu();
        }

        return true;
    }

    private void setChoiceMode(int choiceMode) {
        if (choiceMode == ListView.CHOICE_MODE_NONE) {
            mListView.clearChoices();

            int cnt = mListView.getChildCount();

            for (int i = 0; i < cnt; i++) {
                View view = mListView.getChildAt(i);
                if (view instanceof Checkable) {
                    ((Checkable) view).setChecked(mSelectAll);
                }
            }
        }

        mListView.setChoiceMode(choiceMode);

        if (!AppFeatures.isSupportIntensity()) {
            mListView.setHapticFeedbackEnabled(false);
        }
    }

    public long getPackedPosition() {
        return mPackedPosition;
    }

    public void setPackedPosition(int position) {
        mPackedPosition = mListView.getExpandableListPosition(position);
    }

    public StorageType getEnteredStorageType() {
        return (mPreRecord != null) ? mPreRecord.getStorageType() : StorageType.None;
    }

    private CloudType getEnteredCloudType() {
        if (StorageType.Cloud == getEnteredStorageType()) {
            return ((CloudFileRecord) mPreRecord).getCloudType();
        }
        return CloudType.None;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        setActionMode(ActionModeType.NORMAL);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        setFastScrollEnabled(false);

        if (mAdapter != null) {
            mAdapter.changeCursor(null);
        }

        if (mCloudMgr != null) {
            mCloudMgr.removeListener(this);
        }
    }


    @Override
    public void onCloudInit() {
        //Do nothing
    }

    @Override
    public void onAccountChanged(CloudType cloudType) {
        if (mNavigationInfo != null && !mCloudMgr.isSignedIn(cloudType)) {
            switch (getEnteredStorageType()) {
                case Home:
                    reload();
                    break;
                case Cloud:
                    CloudType enteredCloudType = getEnteredCloudType();
                    if (CloudType.None != enteredCloudType && cloudType == enteredCloudType) {
                        MyFilesFacade.goHome(mProcessId, mNavigationInfo, mContext);
                    }
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (mActivity != null && UiUtils.isDensityDpiChanged(newConfig, mActivity.getDensityDpi())) {
            SelectionManager.getInstance(mProcessId).savePositions(getCheckedItemPositions());
            SelectionManager.getInstance(mProcessId).saveActionModeType(mActionModeType);
            SelectionManager.getInstance(mProcessId).saveExpandedGroupPositions(getExpandedGroupPositions());
            if (isSelectActionMode()) {
                SelectionManager.getInstance(mProcessId).saveGroupPosition(
                        ExpandableListView.getPackedPositionGroup(mPackedPosition));
            }
        }

        super.onConfigurationChanged(newConfig);
    }

    private ArrayList<Integer> getCheckedItemPositions() {
        ArrayList<Integer> selectedPositions = new ArrayList<>();
        SparseBooleanArray selectedChild = mListView.getCheckedItemPositions();
        if (selectedChild != null && selectedChild.size() > 0) {
            for (int flatPosition = 0; flatPosition < selectedChild.size(); flatPosition++) {
                if (selectedChild.valueAt(flatPosition)) {
                    selectedPositions.add(selectedChild.keyAt(flatPosition));
                }
            }
        }

        return selectedPositions;
    }

    private List<Integer> getExpandedGroupPositions() {
        List<Integer> expandedPositions = new ArrayList<>();
        for (int groupPosition = 0; groupPosition < mAdapter.getGroupCount(); groupPosition++) {
            if (mListView.isGroupExpanded(groupPosition)) {
                expandedPositions.add(groupPosition);
            }
        }

        return expandedPositions;
    }

    public void setItemChecked(int position, boolean check) {
        if (position >= 0) {
            mListView.setItemChecked(position, check);
        }
        checkSelectedFileCount(position);
    }

    private void checkSelectedFileCount(int position) {
        if (getSelectedFileCount() == 0) {
            selectAll(false);
        } else {
            if (isNothingSelected()) {
                mPackedPosition = mListView.getExpandableListPosition(position);
            }
        }
    }

    private void setItemsChecked(ArrayList<Integer> selectedChild, boolean check) {
        if (selectedChild != null && !selectedChild.isEmpty()) {
            mListView.clearChoices();
            for (Integer position : selectedChild) {
                setItemChecked(position, check);
            }
        }
    }

    private void setItemsChecked(SparseBooleanArray selectedChild, boolean check) {
        if (selectedChild != null && selectedChild.size() > 0) {
            mListView.clearChoices();
            for (int position = 0; position < selectedChild.size(); position++) {
                if (selectedChild.valueAt(position)) {
                    setItemChecked(selectedChild.keyAt(position), check);
                }
            }
        }
    }

    public boolean isItemChecked(int groupPosition, int childPosition) {
        if (isNothingSelected()) {
            return false;
        }
        boolean isChecked = false;
        int flatPosition = mListView.getFlatListPosition(
                ExpandableListView.getPackedPositionForChild(groupPosition, childPosition));
        SparseBooleanArray selectedChildArray = mListView.getCheckedItemPositions();

        if (selectedChildArray != null
                && selectedChildArray.size() > 0
                && selectedChildArray.get(flatPosition)) {
            int index = selectedChildArray.indexOfKey(flatPosition);
            if (selectedChildArray.valueAt(index)) {
                isChecked = true;
            }
        }

        return isChecked;
    }

    private void sendLogSA(int position) {
        FileRecord record = getFileRecord(position);
        if (record.isDirectory()) {
            SamsungAnalyticsLog.sendLog(getProcessId(), SamsungAnalyticsLog.Event.LONG_PRESS,
                    "Folder", null);
        } else {
            SamsungAnalyticsLog.sendLog(getProcessId(), SamsungAnalyticsLog.Event.LONG_PRESS,
                    "File", null);
        }
    }

    public int getExpandingGroupPosition() {
        return mExpandingGroupPosition;
    }

    public void setExpandingGroupPosition(int groupPosition) {
        mExpandingGroupPosition = groupPosition;
    }

    private void updateMenuView() {
        if (mActivity != null) {
            FileListActionMenu actionMenu = mActivity.getActionMenu();
            if (isSelectActionMode() && actionMenu != null) {
                actionMenu.updateSelectedItemView();
            }
        }
    }

    public boolean isNothingSelected() {
        return mPackedPosition == -1;
    }
}